package com.example.myapplication.a_mvp.model;

import com.example.myapplication.a_mvp.contract.MyContract;
import com.example.myapplication.a_utils.MyRetrofit;
import com.example.myapplication.a_utils.Ret_API;
import com.example.myapplication.b_entity.Login_Entity;
import com.example.myapplication.b_entity.Daxia;
import com.example.myapplication.b_entity.Reg_Entity;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class MyModel implements MyContract.MyModel {

    @Override
    public void reg(String name, String pass, Observer<Reg_Entity> observer) {
        MyRetrofit.getInstance().getRetrofit("http://49.233.0.68:8080/")
                .create(Ret_API.class)
                .reg(name,pass)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    @Override
    public void login(String name, String pass, Observer<Login_Entity> observer) {
        MyRetrofit.getInstance().getRetrofit("http://49.233.0.68:8080/")
                .create(Ret_API.class)
                .login(name,pass)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void daxia(int index,int index2,Observer<Daxia> observer) {
        Retrofit build = new Retrofit.Builder().baseUrl("http://www.qubaobei.com/")
                .client(new OkHttpClient.Builder().addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        build.create(Ret_API.class)
                .getDaxia(index,index2)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);

    }


    @Override
    public void destroy() {

    }
}
